import * as THREE from 'three';
import { Scene } from './Scene';
import { Loop } from './Loop';
import { CameraController } from './controls/CameraController';
import { TestCharacter } from '../characters/TestCharacter';

class Game {
    constructor() {
        // 创建容器
        this.container = document.querySelector('#game-container');
        
        // 初始化渲染器
        this.renderer = new THREE.WebGLRenderer({
            antialias: true,
        });
        this.renderer.setPixelRatio(window.devicePixelRatio);
        this.renderer.setSize(window.innerWidth, window.innerHeight);
        this.container.appendChild(this.renderer.domElement);

        // 创建场景
        this.scene = new Scene();
        
        // 创建相机
        this.camera = new THREE.PerspectiveCamera(
            45,
            window.innerWidth / window.innerHeight,
            0.1,
            1000
        );
        this.camera.position.set(0, 5, 10);
        this.camera.lookAt(0, 0, 0);

        // 创建游戏循环
        this.loop = new Loop(this.camera, this.scene, this.renderer);
        
        // 创建相机控制器
        this.cameraController = new CameraController(this.camera, this.renderer.domElement);
        this.loop.updatables.push(this.cameraController);
        
        // 创建测试角色
        this.testCharacter = new TestCharacter({
            position: new THREE.Vector3(0, 1.5, 0)
        });
        this.testCharacter.init().then(() => {
            this.scene.add(this.testCharacter);
            this.loop.updatables.push(this.testCharacter);
            
            // 播放待机动画
            this.testCharacter.playAnimation('idle');
        });
        
        // 监听窗口调整
        window.addEventListener('resize', this.onWindowResize.bind(this));
        
        // 添加测试按键监听
        this.setupTestControls();
    }

    setupTestControls() {
        // 移动控制
        const keys = {
            KeyW: false,
            KeyS: false,
            KeyA: false,
            KeyD: false
        };

        // 按键监听
        window.addEventListener('keydown', (event) => {
            if (event.code in keys) {
                keys[event.code] = true;
            }
            switch(event.code) {
                case 'Space':
                    this.testCharacter.attack();
                    break;
            }
        });

        window.addEventListener('keyup', (event) => {
            if (event.code in keys) {
                keys[event.code] = false;
            }
        });

        // 添加移动更新到游戏循环
        this.loop.updatables.push({
            tick: () => {
                const direction = new THREE.Vector3();
                
                if (keys.KeyW) direction.z -= 1;
                if (keys.KeyS) direction.z += 1;
                if (keys.KeyA) direction.x -= 1;
                if (keys.KeyD) direction.x += 1;

                // 标准化方向向量
                if (direction.length() > 0) {
                    direction.normalize();
                }
                
                // 更新角色移动
                this.testCharacter.move(direction);
            }
        });
    }

    onWindowResize() {
        this.camera.aspect = window.innerWidth / window.innerHeight;
        this.camera.updateProjectionMatrix();
        this.renderer.setSize(window.innerWidth, window.innerHeight);
    }

    start() {
        this.loop.start();
    }

    stop() {
        this.loop.stop();
    }
}

// 启动游戏
const game = new Game();
game.start();

export { Game }; 